//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"
	"strings"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_requirement"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
)

func processRequirementEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		createRequirementEvent := &events_requirement.CreateRequirementEvent{}
		if event.EventData.MessageIs(createRequirementEvent) {
			err = event.EventData.UnmarshalTo(createRequirementEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 创建需求 %s。", event.UserDisplayName, event.ProjectName,
				createRequirementEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.CreateRequirement {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateRequirementEvent := &events_requirement.UpdateRequirementEvent{}
		if event.EventData.MessageIs(updateRequirementEvent) {
			err = event.EventData.UnmarshalTo(updateRequirementEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 修改需求 %s(原标题 %s)。", event.UserDisplayName, event.ProjectName,
				updateRequirementEvent.NewTitle, updateRequirementEvent.OldTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.UpdateRequirement {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateTagEvent := &events_requirement.UpdateTagEvent{}
		if event.EventData.MessageIs(updateTagEvent) {
			err = event.EventData.UnmarshalTo(updateTagEvent)
			if err != nil {
				return false, err
			}
			oldTagList := []string{}
			newTagList := []string{}
			for _, tag := range updateTagEvent.OldTagList {
				oldTagList = append(oldTagList, tag.TagName)
			}
			for _, tag := range updateTagEvent.NewTagList {
				newTagList = append(newTagList, tag.TagName)
			}
			msg = fmt.Sprintf("%s 在项目 %s 修改需求 %s 标签 %s (原标签 %s)。", event.UserDisplayName, event.ProjectName,
				updateTagEvent.Title, strings.Join(newTagList, ","), strings.Join(oldTagList, ","))
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.UpdateTag {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeRequirementEvent := &events_requirement.RemoveRequirementEvent{}
		if event.EventData.MessageIs(removeRequirementEvent) {
			err = event.EventData.UnmarshalTo(removeRequirementEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 把需求 %s 移入回收站。", event.UserDisplayName, event.ProjectName,
				removeRequirementEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.RemoveRequirement {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		linkIssueEvent := &events_requirement.LinkIssueEvent{}
		if event.EventData.MessageIs(linkIssueEvent) {
			err = event.EventData.UnmarshalTo(linkIssueEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 关联需求 %s 和 任务 %s。", event.UserDisplayName, event.ProjectName,
				linkIssueEvent.Title, linkIssueEvent.IssueTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.LinkIssue {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		unlinkIssueEvent := &events_requirement.UnlinkIssueEvent{}
		if event.EventData.MessageIs(unlinkIssueEvent) {
			err = event.EventData.UnmarshalTo(unlinkIssueEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 取消关联需求 %s 和 任务 %s。", event.UserDisplayName, event.ProjectName,
				unlinkIssueEvent.Title, unlinkIssueEvent.IssueTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.UnlinkIssue {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		closeRequirementEvent := &events_requirement.CloseRequirementEvent{}
		if event.EventData.MessageIs(closeRequirementEvent) {
			err = event.EventData.UnmarshalTo(closeRequirementEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 关闭需求 %s。", event.UserDisplayName, event.ProjectName,
				closeRequirementEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.CloseRequirement {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		openRequirementEvent := &events_requirement.OpenRequirementEvent{}
		if event.EventData.MessageIs(openRequirementEvent) {
			err = event.EventData.UnmarshalTo(openRequirementEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 打开需求 %s。", event.UserDisplayName, event.ProjectName,
				openRequirementEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.OpenRequirement {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		setKanoInfoEvent := &events_requirement.SetKanoInfoEvent{}
		if event.EventData.MessageIs(setKanoInfoEvent) {
			err = event.EventData.UnmarshalTo(setKanoInfoEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新需求 %s kano分析。", event.UserDisplayName, event.ProjectName,
				setKanoInfoEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.SetKanoInfo {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		setFourQInfoEvent := &events_requirement.SetFourQInfoEvent{}
		if event.EventData.MessageIs(setFourQInfoEvent) {
			err = event.EventData.UnmarshalTo(setFourQInfoEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新需求 %s 四象限分析。", event.UserDisplayName, event.ProjectName,
				setFourQInfoEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.RequirementEvCfg.SetFourQInfo {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
